const express = require('express');
const router = express.Router();
const pool = require('../pool');
const jwt = require("jsonwebtoken");
const JWT_SECRET_KEY = "JWT_SECRET_KEY";

router.post('/login',(req,res,next)=>{
    let user = req.body.username;
    let pwd  = req.body.password;
    pool.query(`select * from admin where username=? and pwd=${pwd}`,[user],(err,r)=>{
        if(err){
            return next(err)
        };
        if(r.lenth !=0){
            let user =  {a_id:r[0].a_id,username:r[0].username,a_role:r[0].a_role};
            let token = jwt.sign(user,JWT_SECRET_KEY,{expiresIn:'7d'})
            res.send({code:200,msg:'登陆成功',data:{user,token}})
        }
    })
})

router.get('/Statistical',(req,res,next)=>{
    pool.query('select count(isLive) from user where isLive=true',(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,data:r})
    })
})

module.exports = router;